package springserviceconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import springserviceconsumer.entity.user;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@RestController
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/consumer")
    public String consumer1(){
        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://SPRINGCLOUD-SERVICE-PROVIDER/service/hello", String.class);
        System.out.println(forEntity.getStatusCode()+" "+forEntity.getStatusCodeValue());
       return forEntity.getBody();
    }

    @RequestMapping(value = "/get/consumer")
    public user getUserConsumer(){
        ResponseEntity<user> forEntity = restTemplate.getForEntity("http://SPRINGCLOUD-SERVICE-PROVIDER/service/user", user.class);
        return forEntity.getBody();
    };

    @RequestMapping(value = "/param/consumer")
    public user paramUserConsumer(){

        String[] arr={"20","张三","12035466"};
        ResponseEntity<user> forEntity = restTemplate.getForEntity("http://SPRINGCLOUD-SERVICE-PROVIDER/service/userCanShu?age={0}&name={1}&phone={2}", user.class,arr);
        return forEntity.getBody();
    };

    @RequestMapping(value = "/map/consumer")
    public user mapUserConsumer(){
        Map<String,Object> paramMap=new ConcurrentHashMap<>();
        paramMap.put("age",20);
        paramMap.put("name","李四");
        paramMap.put("phone","1235026");
        ResponseEntity<user> forEntity = restTemplate.getForEntity("http://SPRINGCLOUD-SERVICE-PROVIDER/service/userCanShu?age={age}&name={name}&phone={phone}", user.class,paramMap);
        return forEntity.getBody();
    };

    @RequestMapping("/post/consumer")
    public user postConsumer() {
//        String[] arr = {"20", "张三", "12035466"};
        MultiValueMap<String, Object> multivaluedMap = new LinkedMultiValueMap<>();
        multivaluedMap.add("age",20);
        multivaluedMap.add("name","张三");
        multivaluedMap.add("phone","10235622");

        ResponseEntity<user> forEntity = restTemplate.postForEntity("http://SPRINGCLOUD-SERVICE-PROVIDER/post/user1",multivaluedMap,user.class);
        return forEntity.getBody();

    }

    @RequestMapping("/post/user/consumer")
    public user postUserConsumer(){

        user userPost=new user();
        userPost.setAge(23);
        userPost.setName("王五");
        userPost.setPhone("2132453");
        ResponseEntity<user> forEntity = restTemplate.postForEntity("http://SPRINGCLOUD-SERVICE-PROVIDER/post/user2",userPost,user.class);
        return forEntity.getBody();
    };
}
